<?php
/*
程序BY 边缘狂人 
调用方法
$mem = Init('Ext.Memcached');
$mem->host('3.goodlk.com')->port('33233')->setup();
$key = 'dodo';
if ($mem->set($key, date('Ymd H:i:s')))
{
	echo "OK";
}
else 
{
	echo '342';
}
$v = $mem->get($key);

介绍参考:http://blog.csdn.net/ctowoo/archive/2009/10/14/4672649.aspx
<<<<<<< HEAD
=======

>>>>>>> 99ee92205e27178c65499eae375aac6ef8e9d5ae
框架内使用方法
#实例化
$m = $this->memcache->port('11211')->setup();
$key = "aa";
$str = $m->get($key);
if(!$str)
{
	# KEY, VALUE, 有效期,时间, 0 为永不过期
	$m->add($key, "fdsafdsa", time()+8);
}
echo $str;

#删除
$m->delete($key);


*/
class Ext_Memcache
{
	protected $host = '127.0.0.1';
	protected $port = '11211';
	protected $conn = null;
	protected $defineExpire = 3600;
	protected $prefix = '';
	public $connnect_status = array();
	function __construct()
	{
		if (!class_exists('Memcache'))
		{
			exit("PHP不支持Memcache扩展");
		}
	}
	function host($host)
	{
		$this->host = $host;
		return $this;
	}
	function port($port)
	{
		$this->port = $port;
		return $this;
	}
	function prefix($prefix)
	{
		$this->prefix = $prefix;
		return $this;
	}
	function setup()
	{
		//static $connnect_status;
		//为了防止重复链接MEMCACHE SERVER
		$key = "memcache_{$this->host}_{$this->port}";
		if(!$this->conn = getconfig($key))
		{
			$this->conn = new Memcache;
			$this->conn->connect($this->host, $this->port) or die ("Could not connect");
			load::loadClass("config")->setconfig($key,$this->conn);
		}		
		return $this;
	}
	function get($key)
	{
		return $this->conn->get($this->prefix.$key);
	}
/*	强制写一个值*/
	function set($key = null, $data, $expire = 0)
	{
		return $this->conn->set($key, $data, MEMCACHE_COMPRESSED, $expire);
	}
/*	如果不存在,写一个值,否则返回false*/
	function add($key = null, $data, $expire = 0)
	{
		return $this->conn->add($key, $data, MEMCACHE_COMPRESSED, $expire);
		//return $this->conn->add($this->prefix.$key, $data, false, 10);
	}
	function delete($key)
	{
		return $this->conn->delete($key);
	}
	function key()
	{

	}
	function close()
	{
		$this->conn->close();
	}
	function addServer()
	{
		
	}
	/**清洗（删除）已经存储的所有的元素*/
	function flush()
	{
		$this->conn->flush();
	}
	function version()
	{
		return $this->conn->getVersion ();
	}
	function status()
	{
		return $this->conn->getStats();
	}
	function s()
	{
		$data = $this->status();
		$lang = array(
		'pid' => '进程ID',
		'uptime' => '',
		'time' => '',
		'version' => '版本',
		'pointer_size' => '',
		'rusage_user' => '',
		'rusage_system' => '',
		'curr_connections' => '当前链接数',
		'total_connections' => '总计链接数',
		'connection_structures' => '',
		'cmd_get' => 'GET次数',
		'cmd_set' => 'SET次数',
		'cmd_flush' => '刷新次数',
		'get_hits' => '查询命中次数',
		'get_misses' => '查询失败次数',
		'delete_misses' => '删除失败次数',
		'delete_hits' => '删除命中次数',
		'incr_misses' => '',
		'incr_hits' => '',
		'decr_misses' => '',
		'decr_hits' => '',
		'cas_misses' => '',
		'cas_hits' => '',
		'cas_badval' => '',
		'auth_cmds' => '',
		'auth_errors' => '',
		'bytes_read' => '总计读取数据量',
		'bytes_written' => '总计写入数据量',
		'limit_maxbytes' => '最大允许内存',
		'accepting_conns' => '',
		'listen_disabled_num' => '',
		'threads' => '进程数',
		'conn_yields' => '',
		'bytes' => '占用大小',
		'curr_items' => '当前记录数',
		'total_items' => '历史总记录数',
		'evictions' => '',
		'reclaimed' => '',


		'userp' => '平均每次请求缓存次数',
		'hitp' => '缓存命中率',
		);
		
		$cnt = 1;
		$data['time'] = date('Ymd H:i:s', $data['time']);
		$data['bytes_read'] = round($data['bytes_read']/1024/1024, 2) . 'M';
		$data['bytes_written'] = round($data['bytes_written']/1024/1024, 2) . 'M';
		$data['limit_maxbytes'] = round($data['limit_maxbytes']/1024/1024, 2) . 'M';
		$data['bytes'] = round($data['bytes']/1024/1024, 2) . 'M';
		$data['userp'] = round($data['cmd_get']/$data['uptime'], 2);
		$data['hitp'] = round($data['get_hits']/$data['cmd_get'], 2);
		
		
		echo "<table border=1 width=700px>";
		foreach ($data as $k => $v)
		{
			if (!$lang[$k])
			{
				continue;
			}
			echo "<tr>";
			echo "<td>{$cnt}.{$k} {$lang[$k]} </td>  <td>$v</td>";
			$cnt++;
			echo "</tr>";
		}
		foreach ($data as $k => $v)
		{
			if ($lang[$k])
			{
				continue;
			}
			echo "<tr>";
			echo "<td>{$cnt}.{$k} {$lang[$k]} </td>  <td>$v</td>";
			$cnt++;
			echo "</tr>";
		}
		echo "</table>";
	}
	function __destruct()
	{
		$this->close();
	}
}